package com.Dec_Five;

import java.util.Arrays;

public class Solution1005 {

    public int largestSumAfterKNegations(int[] nums, int k) {
        Arrays.sort(nums);
        int index=0,sum=0;
        for (int i = 0; i < k; i++) {
            if (nums[index]<0&&index<nums.length-1)
            {
                nums[index]=-nums[index];
                if (nums[index]>Math.abs(nums[index+1])) index++;
                continue;
            }
            nums[index]=-nums[index];
        }
        for (int num :
                nums) {
            sum += num;
        }
        return sum;
    }

    public static void main(String[] args) {
        Solution1005 solution1005=new Solution1005();
        System.out.println(solution1005.largestSumAfterKNegations(new int[]{1},1));
    }

}
